/**
 * 
 */
package vue;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.JFileChooser;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

import controller.IController;

/**
 * @author GRONOWSKI Stephane svan001@aol.com
 * 
 *         igsat 2010
 * 
 */
public class IGSATJMenuBar extends JMenuBar {

	private static final long	serialVersionUID	= 1L;

	public static final String	FILE				= "File";
	public static final String	NEW_SESSION			= "Open CNF";
	public static final String	CLOSE				= "Close application";
	// public static final String CLOSE_SESSION = "Close current session";
	// public static final String FILE = "File";

	private IController			controller			= null;

	private JMenu				file				= null;

	private File				lastDirectory		= new File("./tstcnf");


	/**
	 * 
	 */
	public IGSATJMenuBar() {
		// TODO Auto-generated constructor stub
		super();
		createFileMenu();

	}


	/**
	 * return the controller of the menu bar.
	 * 
	 * @return the controller.
	 */
	public IController getController() {
		return controller;
	}


	/**
	 * set the controller of the menu bar.
	 * 
	 * @param controller
	 *            the controller to set
	 */
	public void setController(IController controller) {
		this.controller = controller;
	}


	public void createFileMenu() {
		file = new JMenu(FILE);

		// item nouvelle partie
		JMenuItem newcnf = new JMenuItem(NEW_SESSION);
		newcnf.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO a tester
				// System.out.println("existe " + lastDirectory.exists() + " " +
				// lastDirectory.getAbsolutePath());
				JFileChooser fc = new JFileChooser(lastDirectory);
				int returnVal = fc.showOpenDialog(null);

				if (returnVal == JFileChooser.APPROVE_OPTION)
				{
					File file = fc.getSelectedFile();
					lastDirectory = new File(file.getParent());
					controller.openSession(file);
				}

			};
		});

		// item quitter
		JMenuItem close = new JMenuItem(CLOSE);
		close.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				controller.close();
			}
		});

		// finalise et ajoute le menu dans la barre
		file.add(newcnf);
		file.addSeparator();
		file.add(close);

		add(file);
	}

}
